#include "starstar.h"
#include "ui_starstar.h"
#include <QPainter>

StarStar::StarStar(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::StarStar)
{
    ui->setupUi(this);
    resize(480, 320);
    setStyleSheet("background-color: rgb(0, 0, 0);");

    for(int i = 0; i < 200; i++)
    {
        x[i] = qrand() % width();
        y[i] = qrand() % height();
    }

    timerId = startTimer(100);
}

StarStar::~StarStar()
{
    delete ui;
}

void StarStar::timerEvent(QTimerEvent *e)
{
    Q_UNUSED(e)
//    if(y < height() - 10)
//        y += 10;
//    else if(y1 < height() - 10)
//        y1 += 10;
//    qDebug("time: y = %d, y1 = %d", y, y1);
    for(int i = 0; i < 200; i++)
    {
       y[i]++;
       if(y[i] > height())
       {
           y[i] = 0;
           x[i] = qrand() % width();
       }
    }
    update();
}

void StarStar::paintEvent(QPaintEvent *e)
{
    Q_UNUSED(e)
    qDebug("paintevent");//q开头是全局函数
    QPainter p(this);//即main函数中定义的类StarStar的对象w的地址

//    if(y >= height() - 10)
//        p.drawRect(x1, y1, 10, 10);
//    p.drawRect(x, y, 10, 10);


   // p.drawRect(40, 30, 10, 10);
   // p.drawRect(50, 30, 10, 10);
   // p.drawRect(50, 40, 10, 10);


    p.setBrush(Qt::black);
    p.drawRect(0, 0, width(), height());//width,height是两个函数，是基类窗口的
    p.setPen(Qt::white);//Qt 大写Q，小写q代表全局设置
    //p.drawLine(0, 0, 100, 150);//y轴从上往下画
    //p.drawRect(0, 0, 100, 150);//x轴从左往右画
    //QRect r(50, 50, 100, 100);
    //p.drawRect(r);
    p.setBrush(Qt::yellow);
    p.drawEllipse(30, 30, 100, 100);
    p.setBrush(Qt::black);
    p.setPen(Qt::black);
    p.drawEllipse(-40, -30, 150, 150);
    p.setPen(Qt::white);

    for(int i = 0; i < 200; i++)
    {
        p.drawText(x[i], y[i], "*");
    }

}
